package client;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import network.Client;
import network.Packet;
import network.Packet_Player;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.PixelFormat;
//import org.lwjgl.util.glu.GLU;

import de.matthiasmann.twl.utils.PNGDecoder;
import de.matthiasmann.twl.utils.PNGDecoder.Format;
import org.lwjgl.util.vector.Matrix4f;

import shared.MouseHook;
import shared.NinePatch;
import shared.Scene;
import shared.SceneManager;
import shared.Tools;
import shared.Vec2f;
import vmo.GameManager;

public class MainScene implements Scene
{
	GameManager p_gmanager;

	Main_Render m_render;
	Main_Sim m_sim;
	
	
//	NinePatch m_testpatch;

	
	public MainScene (Client client, int [] variables)
	{
		ColourMap.getInstance();
		m_sim=new Main_Sim(client);
		m_render=new Main_Render(variables,m_sim);
		m_render.MoveView(m_sim.m_player.getPosition());
		m_sim.setSubordinate(m_render.m_actor_render);
	}
	
	public MainScene (Client client, int [] variables,Packet_Player player)
	{
		ColourMap.getInstance();	
		m_sim=new Main_Sim(client);
		m_render=new Main_Render(variables,m_sim);	
		m_sim.setSubordinate(m_render.m_actor_render);	
		m_sim.HandlePlayer(player);
		m_render.MoveView(m_sim.m_player.getPosition());
		m_sim.m_clock.SyncClock(player.getTimestamp());
		m_sim.m_timestamp=player.getTimestamp();
			
	}
	
	void HandleMovekeys()
	{
				
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN) && !Keyboard.isKeyDown(Keyboard.KEY_LEFT) && !Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && !Keyboard.isKeyDown(Keyboard.KEY_UP))
		{
			m_sim.m_player.Action((byte)1,(byte)0,true);
			m_render.RelocateView(m_sim.m_player.getPosition());		
		
		}

		if (Keyboard.isKeyDown(Keyboard.KEY_UP) && !Keyboard.isKeyDown(Keyboard.KEY_LEFT) && !Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && !Keyboard.isKeyDown(Keyboard.KEY_DOWN)) 
		{
			m_sim.m_player.Action((byte)1,(byte)8,true);	
			m_render.RelocateView(m_sim.m_player.getPosition());	

		}	
		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT) && !Keyboard.isKeyDown(Keyboard.KEY_DOWN) && !Keyboard.isKeyDown(Keyboard.KEY_UP) && !Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
		{
			m_sim.m_player.Action((byte)1,(byte)12,true);
			m_render.RelocateView(m_sim.m_player.getPosition());
		}	
		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && !Keyboard.isKeyDown(Keyboard.KEY_DOWN) && !Keyboard.isKeyDown(Keyboard.KEY_UP) && !Keyboard.isKeyDown(Keyboard.KEY_LEFT))
		{
			m_sim.m_player.Action((byte)1,(byte)4,true);
			m_render.RelocateView(m_sim.m_player.getPosition());			
		}	
			
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN) && Keyboard.isKeyDown(Keyboard.KEY_LEFT) && !Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && !Keyboard.isKeyDown(Keyboard.KEY_UP))
		{
			m_sim.m_player.Action((byte)1,(byte)14,true);
			m_render.RelocateView(m_sim.m_player.getPosition());		
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && Keyboard.isKeyDown(Keyboard.KEY_DOWN) && !Keyboard.isKeyDown(Keyboard.KEY_UP) && !Keyboard.isKeyDown(Keyboard.KEY_LEFT))
		{
			m_sim.m_player.Action((byte)1,(byte)2,true);
			m_render.RelocateView(m_sim.m_player.getPosition());			
		}		
		if (Keyboard.isKeyDown(Keyboard.KEY_UP) && Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && !Keyboard.isKeyDown(Keyboard.KEY_LEFT) &&  !Keyboard.isKeyDown(Keyboard.KEY_DOWN)) 
		{
			m_sim.m_player.Action((byte)1,(byte)6,true);	
			m_render.RelocateView(m_sim.m_player.getPosition());	
		}	

		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT) && Keyboard.isKeyDown(Keyboard.KEY_UP) && !Keyboard.isKeyDown(Keyboard.KEY_DOWN) && !Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
		{
			m_sim.m_player.Action((byte)1,(byte)10,true);
			m_render.RelocateView(m_sim.m_player.getPosition());
		}	
		if (!Keyboard.isKeyDown(Keyboard.KEY_DOWN) && !Keyboard.isKeyDown(Keyboard.KEY_LEFT) && !Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && !Keyboard.isKeyDown(Keyboard.KEY_UP))
		{	
			m_sim.m_player.Action((byte)0, (byte)0,true);
		}
	}
	
	@Override
	public void Update(float dt) 
	{
		m_sim.m_clock.Update(dt);
	//	int catchup=0;
		while (m_sim.m_clock.Double(m_sim.m_timestamp))
		{
			m_sim.UpdateWorld(m_sim.m_timestamp,dt);

			HandleMovekeys();
			//catchup++;
		}
		//System.out.print("client catchups per loop");
		//System.out.print(catchup);
		//System.out.println();
		HandleMovekeys();	
		m_sim.UpdateWorld(m_sim.m_timestamp,dt);	

		
		if (m_sim.m_buildview|| m_sim.ShiftNeeded())
		{
			m_render.Generate();
			m_sim.m_buildview=false;
		}
		
		//controls go here
		
		if (Keyboard.next())
		{
				
				boolean b=false;	
				//screen out the special keys 

		}


		
	}
	





	@Override
	public void Start(MouseHook mouse) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void Draw() {
		// TODO Auto-generated method stub
		m_render.Draw();
	}
	@Override
	public void End() {
		// TODO Auto-generated method stub
		m_render.End();
		m_sim.End();
		
	}

}
